package com.xie.smfs.registry;

import com.xie.smfs.registry.LostStatusEffectInstance;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffect;

import net.minecraft.entity.effect.StatusEffectCategory;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket;
import com.xie.smfs.item.RedGhostCandleItem;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;
import com.xie.smfs.entity.GhostEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.UUID;

public class LostStatusEffect extends StatusEffect {
    private static final Logger LOGGER = LoggerFactory.getLogger(LostStatusEffect.class);

    public LostStatusEffect() {
        super(StatusEffectCategory.HARMFUL, 0x000000); // 有害效果，黑色
    }

    @Override
    public boolean canApplyUpdateEffect(int duration, int amplifier) {
        return true; // 每 tick 都执行效果
    }

    @Override
    public void applyUpdateEffect(LivingEntity entity, int amplifier) {
        if (entity instanceof PlayerEntity player) {
            StatusEffectInstance effectInstance = entity.getStatusEffect(this);
            if (effectInstance != null && effectInstance.getEffectType() == ModEffects.LOST) {
                if (!(effectInstance instanceof LostStatusEffectInstance lostInstance)) {
                    return;
                }
                UUID sourceUuid = lostInstance.getSourceUuid();
                // 手持红色鬼烛时绕过迷失效果
                if (RedGhostCandleItem.isHoldingCandle(player)) {
                    return;
                }
                if (sourceUuid != null && entity.getWorld() instanceof ServerWorld serverWorld) {
                    Entity source = serverWorld.getEntity(sourceUuid);
                    if (source == null) {
                        // 来源实体已消失，移除效果
                        player.removeStatusEffect(ModEffects.LOST);
                        return;
                    }
                    if (!(source instanceof GhostEntity ghost)) {
                        return;
                    }
                    // 检查幽灵是否已被压制
                    if (ghost.isSuppressed()) {
                        player.removeStatusEffect(ModEffects.LOST);
                        return;
                    }
                        // 使用 getter 方法获取鬼域半径
                        double ghostDomainRadius = ghost.getGhostDomainRadius();
                        Vec3d center = ghost.getPos();
                        double distance = player.squaredDistanceTo(center);
                        if (distance > ghostDomainRadius * ghostDomainRadius) {
                            Vec3d direction = player.getPos().subtract(center).normalize();
                            Vec3d newPos = center.add(direction.multiply(ghostDomainRadius - 0.5));
                            player.teleport(newPos.x, newPos.y, newPos.z);
                            // 基于方向向量计算朝向，确保准确转身
                            Vec3d directionToCenter = center.subtract(player.getPos()).normalize();
                            float newYaw = (float) (Math.atan2(directionToCenter.z, directionToCenter.x) * (180 / Math.PI) - 90);
                            player.setYaw(newYaw);
                            player.setHeadYaw(newYaw); // 同步头部朝向
                            if (player instanceof ServerPlayerEntity serverPlayer) {
                                // 发送位置同步数据包
                                double x = player.getX();
                                double y = player.getY();
                                double z = player.getZ();
                                float yaw = player.getYaw();
                                float pitch = player.getPitch();
                                serverPlayer.networkHandler.sendPacket(new PlayerPositionLookS2CPacket(x, y, z, newYaw, player.getPitch(), Collections.emptySet(), 0));
                            } // 强制同步客户端
                        }
                    }
                }
            }
        }
    }